<?php

require_once __DIR__.'/vendor/autoload.php';

// 加载环境变量
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();

// 创建数据库连接
try {
    $pdo = new PDO(
        "mysql:host={$_ENV['DB_HOST']};dbname={$_ENV['DB_DATABASE']};charset=utf8mb4",
        $_ENV['DB_USERNAME'],
        $_ENV['DB_PASSWORD'],
        [
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
            PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
        ]
    );

    echo "✅ 数据库连接成功\n\n";

    // 检查菜单系统相关的表
    $tables = ['menus', 'functions', 'permission_nodes', 'function_permissions', 'role_function_permissions'];

    foreach ($tables as $table) {
        echo "=== {$table} 表结构 ===\n";

        $result = $pdo->query("DESCRIBE {$table}");
        $columns = $result->fetchAll();

        if (empty($columns)) {
            echo "❌ 表 {$table} 不存在\n\n";
            continue;
        }

        foreach ($columns as $column) {
            $null = $column['Null'] == 'YES' ? 'NULL' : 'NOT NULL';
            $default = $column['Default'] !== null ? "DEFAULT '{$column['Default']}'" : '';
            echo sprintf("%-20s %-15s %-8s %s\n",
                $column['Field'],
                $column['Type'],
                $null,
                $default
            );
        }

        // 检查数据
        $countResult = $pdo->query("SELECT COUNT(*) as count FROM {$table}");
        $count = $countResult->fetch()['count'];
        echo "\n📊 数据行数: {$count}\n\n";
    }

} catch (Exception $e) {
    echo "❌ 错误: " . $e->getMessage() . "\n";
}
